<?php

/**
 * Yii2 Shortcuts
 * @author Eugene Terentev <eugene@terentev.net>
 * -----
 * This file is just an example and a place where you can add your own shortcuts,
 * it doesn't pretend to be a full list of available possibilities
 * -----
 */

/**
 * @return int|string
 */
function getMyId() {
    return Yii::$app->user->getId();
}

function getMyUser() {
    return Yii::$app->user;
}

/**
 * @return mixed|null
 * 用户身份
 */
function getUserIdentity() {
    return Yii::$app->user->identity;
    return Yii::$app->user !== null ? Yii::$app->user->identityr : null;
}

function getMyUserName() {
    return !Yii::$app->user->isGuest ? Yii::$app->user->identity->getUserName() : '';
}

function getProfile() {
    return !Yii::$app->user->isGuest ? Yii::$app->user->identity->userProfile : null;
}

function getProfileAvatar($defaultUrl = null) {
    return !Yii::$app->user->isGuest ? Yii::$app->user->identity->userProfile->getAvatar($defaultUrl) : $defaultUrl;
}

/**
 * @param string $view
 * @param array $params
 * @return string
 */
function render($view, $params = []) {
    return Yii::$app->controller->render($view, $params);
}

/**
 * @param $url
 * @param int $statusCode
 * @return \yii\web\Response
 */
function redirect($url, $statusCode = 302) {
    return Yii::$app->controller->redirect($url, $statusCode);
}

/**
 * @param $form \yii\widgets\ActiveForm
 * @param $model
 * @param $attribute
 * @param array $inputOptions
 * @param array $fieldOptions
 * @return string
 */
function activeTextinput($form, $model, $attribute, $inputOptions = [], $fieldOptions = []) {
    return $form->field($model, $attribute, $fieldOptions)->textInput($inputOptions);
}

/**
 * @param string $key
 * @param mixed $default
 * @return mixed
 */
function env($key, $default = false) {

    $value = getenv($key);

    if ($value === false) {
        return $default;
    }

    switch (strtolower($value)) {
        case 'true':
        case '(true)':
            return true;

        case 'false':
        case '(false)':
            return false;
    }

    return $value;
}

/**
 * 替换 Yii::t() function,
 * 此函数不支持第三参数, 如果有则调原接口;
 * @param $catelog
 * @param $message
 * @return string
 */
function t($catelog, $message) {
    return Yii::t($catelog, $message, null, 'zh-CN');
}

/**
 * @param int $ditigal
 */
function random4Code() {
    return mt_rand(1000, 9999);
}

function random6Code() {
    return mt_rand(100000, 999999);
}

/**
 * by holy
 * 点击发送验证码
 * @param type $phone
 * @return type
 */
function getValidateCode($phone) {
    $_code = random4Code();
    $phone = trim($phone);

    Yii::$app->session['MBPhone-' . $phone] = $_code;

    //取短信模板; to do
    $_content = strtr('', '{code}', $_code);
    Yii::$app->sms->send($phone, $_content);
    return $_code;
}

//同时支持 utf-8、用于姓名在显示时进行加密
function cut_name($string, $start = 0, $code = 'UTF-8') {
    if ($code == 'UTF-8') {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
        preg_match_all($pa, $string, $t_string);
        foreach ($t_string[0]as $key => $value) {
            if ($key != 0) {
                $t_string[0][$key] = "*";
            }
        }
        return join('', array_slice($t_string[0], $start, count($t_string[0])));
    }
}

/** 用户信息加上***
 * @param
 * @return string
 * modify by holy add space ;
 */
function encrytionPreg($type, $str) {
    if (empty($str)) {
        return "";
    }
    switch ($type) {
        case 'identityId': //身份证，只保留前2位和最后1位，中间全部加***
            $res = substr($str, 0, 4) . " **** **** ** " . substr($str, -4);
            break;
        case 'Name': //姓名，只加密第1位汉子，后面全部显示
            $res = cut_name($str);
            break;
        case 'Phone': //手机，只保留前3位和最后4位，中间位全部加***
            $res = substr($str, 0, 3) . " **** " . substr($str, -4);
            break;
        case 'Email': //邮箱认证
            $item_array = explode("@", $str);
            if (isset($item_array)) {
                $res = substr($str, 0, 2) . "****@" . $item_array[1];
            }
            break;
        case 'cardNo': //银行卡号验证
            $res = substr($str, 0, 4) . "********" . substr($str, -4);
            break;
        case 6;
            $res = mb_substr($str, 0, 6, 'utf-8') . "********";
            break;
        default:
            break;
    }
    return $res;
}

/**
 * 获取商品名称
 * by liu
 */
function GoodName($goodid) {
    $goodModel = frontend\models\goods\Goods::findOne($goodid);
    if (isset($goodModel)) {
        return $goodModel->goodsName;
    } else {
        return '';
    }
}

/**
 * 0未开奖，1未中奖，2已中奖
 * by liu
 */
function TransisWined($luckDrawTransID) {
    $transModel = frontend\models\luckydrawtrans\LuckyDrawTrans::findOne($luckDrawTransID);
    if (isset($transModel)) {
        return $iswided = isWined($transModel->isWined);
    } else {
        return '';
    }
}

function isWined($isWined) {
    switch ($isWined) {
        case '0':
            $iswided = '未开奖';
            break;
        case '1':
            $iswided = '未中奖';
            break;
        case '2':
            $iswided = '已中奖';
            break;
        default:
            $iswided = '状态';
    }
    return $iswided;
}

function uidAdmin() {//商户搜索
    $uid = Yii::$app->user->identity->id;
    $usergroups = app\modules\groups\models\Useringroups::find()->where(['user_id' => $uid])->one();
    if (isset($usergroups)) {
        return $trees = app\modules\groups\models\TreeForm::findOne($usergroups->groupID);
    }
}

function adminRole() {//判断是否是管理员账号登录
    return Yii::$app->user->can('管理员');
}

//修改结束时间为23:59:59秒
function ModificationTime($endtime) {//不为空参数
    $date_zdy = date('Y-m-d', strtotime($endtime));//截取改变成例2017-05-10
    $date_time = strtotime($date_zdy . "00:00:00");//自定义成2017-05-10: 00:00:00
    if ($date_time == strtotime($endtime)) {//判断自定义和传过来的时间是否一致
        $date_end = strtotime($date_zdy . "23:59:59");
    } else {
        $date_end = strtotime($endtime);
    }
    return $date_end;
}
